<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14 0014
 * Time: 17:24
 */

namespace Admin\Controller;

use Think\Page;

class NewsController extends BaseController
{
    public function newslist()
    {
//        dump($this->permission);die;
        if (IS_POST){
            $data=I('post.');
//            dump($data);die;
            $news=M('news')->where(array('id'=>$data['id']))->find();
            if (!$news){
                $this->ajaxReturn(array('code'=>0,'message'=>'不存在这条消息'));
//                ajax_return(0,'不存在这条消息');
            }
            $res=M('news')->where(array('id'=>$data['id']))->delete();
            if ($res){
                $this->ajaxReturn(array('code'=>1,'message'=>'删除成功'));
//                ajax_return(1,'删除成功');
            }else{
                $this->ajaxReturn(array('code'=>0,'message'=>'删除失败'));
//                ajax_return(0,'删除失败');
            }
        }
//        $count=M('news')->where(array('type'=>1))->count();
        $count=M('news')->where(array('status'=>1,'type'=>array('in',[1,2,3,4])))->count();
        $page=new Page($count,10);
        $show=$page->show();
//        $news=M('news')->where(array('type'=>1))->limit($page->firstRow.','.$page->listRows)->order('create_time desc')->select();
        $news=M('news')->where(array('status'=>1,'type'=>array('in',[1,2,3,4])))->limit($page->firstRow.','.$page->listRows)->order('create_time desc')->select();
        $this->assign('data',$news);
        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->display();
    }


    public function  add_news()
    {
        if (IS_POST){
            $news = M('news');
            $param = I('post.');
            $param['create_time'] =  time();
            $param['content'] = htmlspecialchars_decode($param['content']);

                if(!$news->add($param)){
                    ajax_return(1,'新增失败');
                }


            ajax_return(0,'保存成功',U('newslist'));
        }
        $this->assign('title','添加公告');
        $this->display();

    }


    public function edit_news()
    {
        if (IS_POST){
            $news = M('news');
            $param = I('post.');
            $param['create_time'] =  time();
            $param['content'] = htmlspecialchars_decode($param['content']);

                if($news->save($param) === false){
                    ajax_return(1,'编辑失败');
                }


            ajax_return(0,'保存成功',U('newslist'));
        }
        $this->assign('title','编辑公告');
        $this->assign('data',M('news')->find(I('get.id')));
        $this->display('add_news');

    }


    public function add_edit_news()
    {
        if(IS_POST){
            $news = M('news');
            $param = I('post.');
            $param['create_time'] =  time();
            $param['content'] = htmlspecialchars_decode($param['content']);
            if($param['id']){
                    if($news->save($param) === false){
                        ajax_return(1,'编辑失败');
                    }
            }else{
                if(!$news->add($param)){
                    ajax_return(1,'新增失败');
                }
            }

            ajax_return(0,'保存成功',U('newslist'));
        }
    }

    public function newslist2()
    {
        $this->assign('data',M('news2')->select());
        $this->display();
    }


    public function  add_news2()
    {
        $this->assign('title','添加公告');
        $this->display();

    }


    public function edit_news2()
    {
        $this->assign('title','编辑公告');
        $this->assign('data',M('news2')->find(I('get.id')));
        $this->display('add_news2');

    }


    public function add_edit_news2()
    {
        if(IS_POST){
            $news = M('news2');
            $param = I('post.');
            $param['create_time'] =  time();
            $param['content'] = htmlspecialchars_decode($param['content']);
            if($param['id']){
                if($news->save($param) === false){
                    ajax_return(1,'编辑失败');
                }
            }else{
                if(!$news->add($param)){
                    ajax_return(1,'新增失败');
                }
            }

            ajax_return(0,'保存成功',U('newslist2'));
        }
    }

    /**
     * @Notes:财商学院
     * @author: Qian
     * @Time: 2020/11/26 14:46
     */
    public function college()
    {
//        dump($this->permission);die;
        if (IS_POST){
            $data=I('post.');
//            dump($data);die;
            $news=M('news')->where(array('id'=>$data['id']))->find();
            if (!$news){
                $this->ajaxReturn(array('code'=>0,'message'=>'不存在这条消息'));
//                ajax_return(0,'不存在这条消息');
            }
            $res=M('news')->where(array('id'=>$data['id']))->delete();
            if ($res){
                $this->ajaxReturn(array('code'=>1,'message'=>'删除成功'));
//                ajax_return(1,'删除成功');
            }else{
                $this->ajaxReturn(array('code'=>0,'message'=>'删除失败'));
//                ajax_return(0,'删除失败');
            }
        }
//        $count=M('news')->where(array('type'=>1))->count();
        $count=M('news')->where(array('status'=>1,'type'=>5))->count();
        $page=new Page($count,10);
        $show=$page->show();
//        $news=M('news')->where(array('type'=>1))->limit($page->firstRow.','.$page->listRows)->order('create_time desc')->select();
        $news=M('news')->where(array('status'=>1,'type'=>5))->limit($page->firstRow.','.$page->listRows)->order('create_time desc')->select();
        $this->assign('data',$news);
        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->display();
    }

    /**
     * @Notes:添加财商学院
     * @author: Qian
     * @Time: 2020/11/26 14:51
     */
    public function  add_college()
    {
        if (IS_POST){
            $news = M('news');
            $param = I('post.');
            $param['create_time'] =  time();
            $param['content'] = htmlspecialchars_decode($param['content']);

            if(!$news->add($param)){
                ajax_return(1,'新增失败');
            }


            ajax_return(0,'保存成功',U('college'));
        }
        $this->assign('title','添加财商学院资讯');
        $this->display();

    }

    /**
     * @Notes:编辑财商学院
     * @author: Qian
     * @Time: 2020/11/26 14:56
     */
    public function edit_college()
    {
        if (IS_POST){
            $news = M('news');
            $param = I('post.');
            $param['create_time'] =  time();
            $param['content'] = htmlspecialchars_decode($param['content']);

            if($news->save($param) === false){
                ajax_return(1,'编辑失败');
            }


            ajax_return(0,'保存成功',U('newslist'));
        }
        $this->assign('title','编辑财商学院资讯');
        $this->assign('data',M('news')->find(I('get.id')));
        $this->display('add_college');

    }

    public function chat(){

        $list = M('chat')->order('status asc,new_time desc')->select();
        foreach ($list as $k=>$v){
            $user = M('user')->where(array('id'=>$v['user_id']))->find();
            $list[$k]['username'] = $user['username'];
        }
        $this->assign('list',$list);
        $this->display();
    }

    public function chat_reply(){
        $chat_id = I('get.chat');
        $chat = M('chat')->where(['id'=>$chat_id])->find();
        if ($chat && $chat['new_person']=='member' && $chat['status'] == 0){
            M('chat')->where(array('id'=>$chat['id']))->save(array('status'=>1));
        }
        $reply = M('chat_reply')->where(array('chat_id'=>$chat_id))->order('id asc')->select();
        if (IS_POST){
            $chat_id = I('post.chat_id');
            $content = I('post.content');
            if ($content == ''){
                ajax_return(1,'请输入回复内容');
            }
            $res = M('chat')->where(array('id'=>$chat_id))->save(['status'=>0,'new_content'=>$content,'new_time'=>time(),'new_person'=>'admin']);
            $add['chat_id'] = $chat_id;
            $add['person'] = 'admin';
            $add['content'] = $content;
            $add['time'] = time();
            $res1 = M('chat_reply')->add($add);
            if ($res && $res1){
                ajax_return(0,'回复成功');
            }else{
                ajax_return(1,'回复失败');
            }
        }
        $this->assign('reply',$reply);
        $this->assign('chat_id',$chat_id);
        $this->display();
    }
}

